package com.zhxy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.zhxy.mapper.AdminMapper;
import com.zhxy.pojo.Admin;
import com.zhxy.service.AdminService;
import com.zhxy.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)  //提升性能
    public Admin login(String name, String password) throws Exception {
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
       // String encryptPwd = MD5.encrypt(password);//相同的密码加密后，密文一定是相同的
      //  queryWrapper.eq("name",name)
       //             .eq("password",encryptPwd);
        String encodePwd = bCryptPasswordEncoder.encode(password);//相同的密码加密后，密文每次是不相同的
        System.out.println(encodePwd);
        queryWrapper.eq("name",name);
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin==null){
            throw new Exception("用户名错误！");
        }

        boolean matches = bCryptPasswordEncoder.matches(password, admin.getPassword());
        if(!matches){
            throw new Exception("密码错误！");
        }
        return admin;






    }
}
